package com.leetcode.array;

/**
 * 双指针碰撞的问题  ==》 这个题为什么这么慢。。。。慢得一逼啊~~~
 */
public class Leetcode125 {
    public boolean isPalindrome(String s) {
        if (s == null || s.trim().length() == 0) {
            return true;
        }
        s = s.toLowerCase(); // 因为不考虑大小写，全部默认小写
        int left = 0;
        int right = s.length() - 1;
        while (left <= right) {
            char lChar = s.charAt(left);
            if (lChar < '0' || lChar > '9' && lChar < 'a' || lChar > 'z') {
                left ++;
               continue;
            }
            char rChar = s.charAt(right);
            if (rChar < '0' || rChar > '9' && rChar < 'a' || rChar > 'z') {
                right --;
               continue;
            }
            if (lChar != rChar) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    /**
     * 这个效率相当的低啊
     * @param s
     * @return
     */
    public boolean isPalindrome_bak(String s) {
        if (s == null || s.trim().length() == 0) {
            return true;
        }
        s = s.toLowerCase(); // 因为不考虑大小写，全部默认小写
        int left = 0;
        int right = s.length() - 1;
        while (left <= right) {
            char lChar = s.charAt(left);
            while (lChar < '0' || lChar > '9' && lChar < 'a' || lChar > 'z') {
                left ++;
                if (left > right) {
                    return true;
                }
                lChar = s.charAt(left);
            }
            char rChar = s.charAt(right);
            while (rChar < '0' || rChar > '9' && rChar < 'a' || rChar > 'z') {
                right --;
                if (right < left) {
                    return true;
                }
                rChar = s.charAt(right);
            }
            if (lChar != rChar) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }



    public static void main(String[] args) {
        String s = ".";
        Leetcode125 leet = new Leetcode125();
        boolean res = leet.isPalindrome(s);
        System.out.println(res);
    }
}
